/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet.ajax;

import classes.*;
import logic.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Shelling Ford
 */
@WebServlet(name="chat", urlPatterns={"/ajax/chat"})
public class chat extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        // mengambil data dari request
        String content = request.getParameter("message");
        String type = request.getParameter("type");
        String name = request.getParameter("name");
        HttpSession session = request.getSession(true);

        Room recipientRoom = null;
        User recipientUser = null;
        Message message;

        if (type.equalsIgnoreCase("room")) {
            // bikin room
            recipientRoom = Main.getRoomFromName(name);
            // bikin message
            message = new Message((User)session.getAttribute("user"),recipientRoom,content);
        }
        else {
            // bikin user
            recipientUser = Main.getUserFromName(name);
            // bikin message
            message = new Message((User)session.getAttribute("user"),recipientUser,content);
        }

        // tambahkan message ke list
        Main.addMessage(message);

        if (type.equalsIgnoreCase("user")) {
            response.setContentType("text/xml;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
                // tulis response disini
                out.println("<?xml version=\"1.0\"?>");
                out.println("<root>");
                out.println("<message>");
                out.println("<type>"+type+"</type>");
                out.println("<name>"+name+"</name>");
                out.println("<content>"+((User)session.getAttribute("user")).username+" : "+content+"</content>");
                out.println("</message>");
                out.println("</root>");
            } finally {
                out.close();
            }
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
